package cn.jltx.rxjava.rx.learn;

import android.util.Log;

import cn.jltx.rxjava.rx.api.TencentService;
import cn.jltx.rxjava.rx.bean.TencentBean;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/15.
 */

public class RxJava2Learn4 {

    /**
     * RxJava+Retrofit示例
     */
    public static void getNewsIndexRxJava2AndRetrofix(){
        TencentService.getApi().getNewsIndex("banner")
                .subscribeOn(Schedulers.io())
                .doOnNext(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String s) throws Exception {
                        Log.d(XqTag.TAG, "doOnNext.accept.thread:" + Thread.currentThread());
                        Log.d(XqTag.TAG, "doOnNext.accept:" +  s );
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(XqTag.TAG, "onSubscribe.thread:" + Thread.currentThread());
                    }

                    @Override
                    public void onNext(String s) {
                        Log.d(XqTag.TAG, "onNext.thread:" + Thread.currentThread());
                       Log.d(XqTag.TAG, "onNext:" + s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(XqTag.TAG, "onError:" + e.toString() + ".thread:" + Thread.currentThread());
                    }

                    @Override
                    public void onComplete() {
                        Log.d(XqTag.TAG, "onComplete.thread:" + Thread.currentThread());

                    }
                });
    }

    public static void getNewsIndexRxJava2AndRetrofixBean(){
        TencentService.getApi().getNewsIndexBean("banner")
                .subscribeOn(Schedulers.io())
                .doOnNext(new Consumer<TencentBean>() {
                    @Override
                    public void accept(@NonNull TencentBean tencentBean) throws Exception {
                        Log.d(XqTag.TAG, "doOnNext.accept.thread:" + Thread.currentThread());
                        Log.d(XqTag.TAG, "doOnNext.accept:" +  tencentBean.toString() );
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TencentBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(XqTag.TAG, "onSubscribe.thread:" + Thread.currentThread());
                    }

                    @Override
                    public void onNext(TencentBean tencentBean) {
                        Log.d(XqTag.TAG, "onNext.thread:" + Thread.currentThread());
                        Log.d(XqTag.TAG, "onNext:" + tencentBean.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(XqTag.TAG, "onError:" + e.toString() + ".thread:" + Thread.currentThread());
                    }

                    @Override
                    public void onComplete() {
                        Log.d(XqTag.TAG, "onComplete.thread:" + Thread.currentThread());
                    }
                });
    }
}
